﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using Jbe.TestSuite.TestDevices.FunctionGenerator.Interface;
using Jbe.Composite.Extensions;
using System.Windows.Input;
using System.Reflection;

namespace Jbe.TestSuite.TestDevices.FunctionGenerator.ControlView.Applications
{
    internal class FunctionGeneratorPresentationModel : PresentationModel<IFunctionGeneratorView>, IHeaderInfoProvider
    {
        private readonly DelegateCommand configureCommand;
        private readonly DelegateCommand startCommand;
        private readonly DelegateCommand stopCommand;
        private readonly IFunctionGenerator functionGenerator;
        private readonly ConfigurationPresentationModel configPresentationModel;
        private bool isActive;
        private string name;


        public event EventHandler ConfigureClick;


        public FunctionGeneratorPresentationModel(IFunctionGeneratorView view, IFunctionGenerator functionGenerator)
            : base(view)
        {
            this.functionGenerator = functionGenerator;
            this.isActive = functionGenerator.IsActive;
            this.configureCommand = new DelegateCommand(Configure, CanConfigure);
            this.startCommand = new DelegateCommand(Start, CanStart);
            this.stopCommand = new DelegateCommand(Stop, CanStop);
            this.configPresentationModel = new ConfigurationPresentationModel(View.ConfigurationView);
            this.configPresentationModel.Config = functionGenerator.Config;

            PropertyChangedEventManager.AddListener(functionGenerator, this, "");
        }


        public string HeaderInfo
        {
            get { return Name; }
        }

        public ImageSource HeaderIcon
        {
            get { return new BitmapImage(ResourceService.GetPackUri("Resources/cog.ico")); }
        }

        public ICommand ConfigureCommand { get { return configureCommand; } }

        public ICommand StartCommand { get { return startCommand; } }

        public ICommand StopCommand { get { return stopCommand; } }

        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (isActive != value)
                {
                    isActive = value;
                    RaisePropertyChanged("IsActive");
                    configureCommand.RaiseCanExecuteChanged();
                    startCommand.RaiseCanExecuteChanged();
                    stopCommand.RaiseCanExecuteChanged();
                }
            }
        }

        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    RaisePropertyChanged("Name");
                    RaisePropertyChanged("HeaderInfo");
                }
            }
        }


        public bool CanConfigure() { return !IsActive; }

        public void Configure()
        {
            OnConfigureClick(EventArgs.Empty);
        }

        public bool CanStart() { return !IsActive; }

        public void Start()
        {
            functionGenerator.Start();
            IsActive = true;
        }

        public bool CanStop() { return IsActive; }

        public void Stop()
        {
            functionGenerator.Stop();
            IsActive = false;
        }

        protected override void ReceiveWeakEvent(Type managerType, object sender, EventArgs e, ref bool handled)
        {
            base.ReceiveWeakEvent(managerType, sender, e, ref handled);

            if (managerType == typeof(PropertyChangedEventManager) && sender == functionGenerator)
            {
                FunctionGeneratorPropertyChanged(sender, (PropertyChangedEventArgs)e);
                handled = true;
            }
        }

        protected virtual void OnConfigureClick(EventArgs e)
        {
            if (ConfigureClick != null) { ConfigureClick(this, e); }
        }

        private void FunctionGeneratorPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Config")
            {
                configPresentationModel.Config = functionGenerator.Config;
            }
        }
    }
}
